package com.yaorange.springsecuirtydemo.service;

import com.yaorange.springsecuirtydemo.domain.LoginUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author coach tam
 * @email 327395128@qq.com
 * @values 坚持灵活 灵活坚持
 * @since 2024/5/28
 */
@Component
public class MyUserDetailService implements UserDetailsService {

    /**
     * 模拟mysql用户操作
     */
    public static ConcurrentHashMap<String, UserDetails> userDetailsMap = new ConcurrentHashMap<>();


    static {
        GrantedAuthority[] authorities = new GrantedAuthority[2];
        authorities[0] = () -> "student:update";
        authorities[1] = () -> "student:delete";

        UserDetails zs = new LoginUser("zs", "123456", Arrays.asList(authorities));
        UserDetails ls = new LoginUser("ls", "123456", Arrays.asList(authorities));
        userDetailsMap.put("zs", zs);
        userDetailsMap.put("ls", ls);
    }
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserDetails userDetails = userDetailsMap.get(username);
        if (userDetails == null) {
            throw new UsernameNotFoundException(username);
        }
        return userDetails;
    }
}
